﻿using System.Collections;
using System.Text;
using UnityEngine;
using ZSW.Framework;

namespace ZSW.Main
{
    /// <summary>
    /// 全局配置管理
    /// </summary>
    public class ZSWF_GlobalSetting
    {
        private string globalSettingPath => Application.streamingAssetsPath + "/Config.ini";
        public bool IsInited { get; private set; }
        public string WebSocketAddress;
        public string[] TcpSocketAddress;
        public string SmokeDetectorNodeCode;
        public string ManualAlarmNodeCode;
        public int FirecontrlInterval;
        public int RequestInterval;
        public int DeviceStopDelayTime;

        private string StrFromHash(Hashtable hash, string key, string defalut = "")
        {
            if (hash.ContainsKey(key))
            {
                return hash[key].ToString();
            }
            return defalut;
        }

        private int IntFromHash(Hashtable hash, string key, int defalut = 0)
        {
            if (hash.ContainsKey(key))
            {
                return int.Parse(hash[key].ToString());
            }
            return defalut;
        }

        public void InitContainerComponent()
        {
            if (ZSWF_Ini.GetIniContent(globalSettingPath, Encoding.UTF8).TryGetValue("Net", out Hashtable configSection))
            {
                WebSocketAddress = StrFromHash(configSection, "WebSocket", "ws://127.0.0.1:5001");
                TcpSocketAddress = new string[2];
                TcpSocketAddress[0] = StrFromHash(configSection, "TCPSocket0", "127.0.0.1:5002");
                TcpSocketAddress[1] = StrFromHash(configSection, "TCPSocket1", "127.0.0.1:5003");
            }

            if (ZSWF_Ini.GetIniContent(globalSettingPath, Encoding.UTF8).TryGetValue("FireControl", out Hashtable firecontrolSection))
            {
                SmokeDetectorNodeCode = StrFromHash(firecontrolSection, "SmokeDetectorMonitorCode", "00000");
                ManualAlarmNodeCode = StrFromHash(firecontrolSection, "ManualAlarmMonitorCode", "00000");
                FirecontrlInterval = IntFromHash(firecontrolSection, "FirecontrlInterval", 1);
                RequestInterval = IntFromHash(firecontrolSection, "RequestInterval", 5);
                DeviceStopDelayTime = IntFromHash(firecontrolSection, "DeviceStopDelayTime", 30);
            }

            IsInited = true;
        }

    }
}